package com.apabi.web;

import com.apabi.config.Global;
import com.apabi.model.Collections;
import com.apabi.model.Downloads;
import com.apabi.model.User;
import com.apabi.service.CollectionsService;
import com.apabi.service.DownloadsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.servlet.http.HttpSession;

/**
 * Created by huhu on 2018/1/11.
 * 个人中心
 */
@Controller
public class PersonCenterController {

    @Autowired
    private DownloadsService downloadsService;

    @Autowired
    private CollectionsService collectionsService;
    /**
     * 设置
     * @return
     */
    @RequestMapping("/person_setting")
    public  String personSetting(HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        model.addAttribute("user", user);
        return "/personCenter/person_setting";
    }

    /**
     * 收藏
     * @return
     */
    @RequestMapping("/person_collect")
    public String personCollect(@SessionAttribute User user, Model model, @RequestParam(value = "page", defaultValue = "0") Integer page){
        Pageable pageable = new PageRequest(page, Global.PAGE_SIZE);
        Page<Collections> pages = collectionsService.findByUserId(user.getId(), pageable);
        model.addAttribute("pageTotal", pages.getTotalPages());
        model.addAttribute("page", page);
        model.addAttribute("collections", pages.getContent());
        return"/personCenter/person_collect";
    }

    /**
     * 单条收藏删除
     * @param user
     * @param id
     * @return
     */
    @RequestMapping("/delete_collect")
    public String deleteCollect(@SessionAttribute User user, Integer id){
        collectionsService.removeByIdAndUserId(id,user.getId());
        return InternalResourceViewResolver.REDIRECT_URL_PREFIX+"/person_collect";
    }

    /**
     * 清空收藏
     * @param user
     * @return
     */
    @RequestMapping("/empty_collect")
    public String emptyCollect(@SessionAttribute User user){
        collectionsService.removeByUserId(user.getId());
        return InternalResourceViewResolver.REDIRECT_URL_PREFIX+"/person_collect";
    }

    /**
     * 下载
     * @return
     */
    @RequestMapping("/person_down")
    public  String personDown(@SessionAttribute User user, Model model, @RequestParam(value = "page", defaultValue = "0") Integer page){
        Pageable pageable = new PageRequest(page, Global.PAGE_SIZE);
        Page<Downloads> pages = downloadsService.findByUserId(user.getId(), pageable);
        model.addAttribute("pageTotal", pages.getTotalPages());
        model.addAttribute("page", page);
        model.addAttribute("downloads", pages.getContent());
        return "/personCenter/person_down";
    }

    /**
     * 清空下载
     * @param user
     * @return
     */
    @RequestMapping("empty_down")
    public String emptyDown(@SessionAttribute User user) {
        downloadsService.deleteByUserId(user.getId());
        return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/person_down";
    }

    @RequestMapping("delete_down")
    public String deleteDown(@SessionAttribute User user, Integer id) {
        downloadsService.deleteByIdAndUserId(id, user.getId());
        return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/person_down";
    }

    /**
     * 修改手机
     * @return
     */
    @RequestMapping("/person_setting_update_phone")
    public  String personSettingUpdatePhone(){
        return "/personCenter/person_setting_update_phone";
    }

    /**
     * 修改密码
     * @return
     */
    @RequestMapping("/person_setting_update_pwd")
    public  String personSettingUpdatePwd(){
        return "/personCenter/person_setting_update_pwd";
    }
}
